<?php

namespace app\api\controller;

use app\api\BaseController;
use app\api\middleware\Auth;
use app\api\validate\ApproveValidate;
use app\load\loadModel\OaModel;
use app\load\loadServices\AdminServices;
use app\load\loadServices\OaServices;
use app\oa\validate\FlowValidate;
use think\Exception;
use think\exception\ValidateException;

/**
 * Oa办公
 */
class Oa extends BaseController
{
    /**
     * 免登录方法
     * 控制器中间件 [登录 不需要鉴权]
     * @var array
     */
    protected $middleware = [
        Auth::class => ['except' => []]
    ];

    /**
     * 列表
     * @return void
     */
    public function list()
    {
        $param = input();
        try {

            $where[] = ['admin_id', '=', JWT_UID];

            if (!empty($param['check_status'])) {
                if ($param['check_status'] == 1) {
                    $where[] = ['check_status', '<', 2];
                }
                if ($param['check_status'] == 2) {
                    $where[] = ['check_status', '=', 2];
                }
                if ($param['check_status'] == 3) {
                    $where[] = ['check_status', '>', 2];
                }
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];

            $list = OaModel::Approve()->list(
                $where,
                $rows,
                $param,
                'f.id,f.create_time,a.name,f.check_status,d.title as department_name,t.title as flow_type');

            $this->apiSuccess('请求成功', $list);
        } catch (Exception $e) {
            $this->apiError($e->getMessage());
        }
    }


    /**
     * 详情
     * @return void
     */
    public function info()
    {
        $input = input();
        try {
            validate(ApproveValidate::class)->scene('info')->check($input);
            $result = OaServices::Oa()->info(
                ['id' => $input['id']],
                'id,name,content,create_time,admin_id,check_status,detail_time,flow_id,is_sign,type');
            $this->apiSuccess('请求成功', $result);
        } catch (ValidateException|Exception $e) {
            $this->apiError($e->getMessage());
        }
    }

    /**
     * 审核
     * [    id: 32
     * content: 123
     * check_node: 1
     * check_admin_ids: 0
     * check: 1  |状态  成功  拒绝   撤回
     * type: 1  | 类型  日常审核  一个报销审批  一个发票审批  一个合同审批(暂未开发)
     * file: storage/base64_image/64d59caa2c05f.png
     * ]
     * @return void
     */
    public function check()
    {
        $input = input();
        try {
            //参数必传入项
            validate(FlowValidate::class)->scene('check')->check($input);
            OaServices::Flow()->save($input, JWT_UID);
            $this->apiSuccess('审核成功');
        } catch (ValidateException|Exception $e) {
            $this->apiError($e->getMessage());
        }
    }

    /**
     * 添加审核
     * @return void
     */
    public function save()
    {
        $param = input();
        try {
            if (isset($param['detail_time'])) {
                $param['detail_time'] = strtotime($param['detail_time']);
            }

            if (isset($param['start_time'])) {
                $param['start_time'] = strtotime($param['start_time']);
            }

            if (isset($param['end_time'])) {
                $param['end_time'] = strtotime($param['end_time']);
                if ($param['end_time'] < $param['start_time']) {
                    return to_assign(1, "时间选择有误");
                }
            }

            if (isset($param['start_time_a'])) {
                $param['start_time'] = strtotime($param['start_time_a'] . '' . $param['start_time_b']);
            }

            if (isset($param['end_time_a'])) {
                $param['end_time'] = strtotime($param['end_time_a'] . '' . $param['end_time_b']);
                if ($param['end_time'] <= $param['start_time']) {
                    return to_assign(1, "结束时间需要大于开始时间");
                }
            }

            if (empty($param['flow_id'])) {
                return to_assign(1, "请选择审批类型");
            }
            $info = AdminServices::Admin()->info(['id' => JWT_UID]);
            OaServices::Oa()->save($param, JWT_UID, $info['did']);
            return to_assign();
        } catch (Exception $e) {
            $this->apiError($e->getMessage());
        }
    }
}